﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

namespace YXShop.Web.admin.shop
{
    public partial class shop_color_edit : System.Web.UI.Page
    {
        YXShop.BLL.Shop.ShopColor colorBll = new YXShop.BLL.Shop.ShopColor();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitWebControl();
                string id = ChangeHope.WebPage.PageRequest.GetQueryString("colorId");
                if (id != null && id != "")
                {
                    this.txtTemplatePath.Enabled = false;
                    ViewState["colorId"] = id;
                    YXShop.Model.Shop.ShopColor model = colorBll.GetModel(Convert.ToInt32(id));
                    if (model != null)
                    {
                        this.txtName.Text = model.Name;
                        this.txtDesc.Text = model.Depict;
                        this.txtTemplatePath.Text = model.Templatepath;
                        this.rblType.SelectedValue = model.Isstate.ToString();
                        ViewState["Images"] = model.Samplemappath;
                        ViewState["Samplemappath"] = model.Templatepath;
                    }
                }
            }
        }

        #region 验证
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtName, "风格名称不能为空", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtDesc, "描述不能为空", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtTemplatePath, "风格模板路径不能为空,只输入文件夹名", "isnull", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        #endregion

        #region 添加与修改数据
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void Save()
        {
            YXShop.Model.Shop.ShopColor model = new YXShop.Model.Shop.ShopColor();
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();

            string fileName = ChangeHope.Common.FileHelper.StringToHtml(this.txtTemplatePath.Text.ToString());
            DirectoryInfo info = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/templates/store_template"));
            foreach (DirectoryInfo item in info.GetDirectories())
            {
                if (fileName == item.ToString())
                {
                    this.ltlMsg.Text = "操作失败，您创建文件夹已存在。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            ChangeHope.Common.FileHelper file = new ChangeHope.Common.FileHelper();
            string dirName = HttpContext.Current.Server.MapPath("~/") + "templates\\store_template\\" + fileName + "\\";
            if (!file.CreateDirectory(dirName))
            {
                this.ltlMsg.Text = "操作失败，您创建文件夹失败。";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }

            //图片参数
            string fileStr = "";
            //店铺样式图片
            string path = "/yxuploadfile/shop/mode";
            //原图
            ChangeHope.Common.UploadFile uf = new ChangeHope.Common.UploadFile();
            uf.ExtensionLim = ".gif,.jpg,.jpeg,.bmp";
            uf.FileLengthLim = 4000;
            uf.PostedFile = this.FileUploadPic;
            uf.SavePath = path;
            if (uf.Upload())
            {
                if (uf.HaveLoad)
                {
                    model.Samplemappath = uf.FilePath;
                }
                else
                {
                    if (ViewState["Images"] != null)
                    {
                        model.Samplemappath = ViewState["Images"].ToString();
                    }
                    else
                    {
                        model.Samplemappath = string.Empty;
                    }
                }
            }
            else
            {
                this.ltlMsg.Text = "操作失败，" + uf.Message + "";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
                model.Templatepath ="templates/store_template/"+fileName;
            model.Name = this.txtName.Text.Trim().ToString();
            model.Depict = this.txtDesc.Text.Trim().ToString();
            model.Version = sp.Versions.ToString();
            model.Isstate = Convert.ToInt32(this.rblType.SelectedValue);
            model.CrateTime = Convert.ToDateTime(System.DateTime.Now);
            if (ViewState["colorId"] != null)
            {
                model.Id = Convert.ToInt32(ViewState["colorId"]);
                colorBll.Update(model);
                this.ltlMsg.Text = "操作成功，已修改该信息";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";

            }
            else
            {
                int count = colorBll.Add(model);
                if (count > 0)
                {
                    this.ltlMsg.Text = "操作成功，已添加该风格";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                }
                else
                {
                    this.ltlMsg.Text = "操作失败";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                }
            }

        }
        #endregion

    }
}
